import org.jsecurity.crypto.hash.Sha1Hash
class CreateSiteService {
    boolean transactional = true

    def createTeacherSite(option) {
        def school = School.findByHostId(option.schoolId)
        if(!school){
            return ['success':false,'message' : '学校没有找到，请联系管理员生成此学校！']
        }
        def role;
        if(option.role == '1'){// 老师
            if(option.hostId && Teacher.countByHostId(option.hostId) < 1){
                role = new Teacher()
            }
        }else if(option.role == '2'){//学生
            if(option.hostId && Student.countByHostId(option.hostId) < 1){
                role = new Student()
            }
        }else {
            if(option.hostId && Classer.countByHostId(option.hostId) < 1){
                role = new Classer()
            }
        }
        if(role){
            saveRole(option,role,school)
        }else{
            if(option.role == '3'){
                def classerInstance = Classer.findByHostId(option.hostId)
                if(classerInstance && classerInstance.master != option.master.toLong()){
                    classerInstance.master = option.master.toLong()
                    classerInstance.username = URLDecoder.decode(option.username, "UTF-8")
                    if (classerInstance.hasErrors() || !classerInstance.save(flush: true)) {
                      return ['success':false,'message' : '班级的班主任信息更新成功！']
                    }
                }
            }
        }

        return ['success':true,'message' : "此用户的blog已经存在"]
    }

    def saveRole(option,role,school){
        if(role in Classer){
            log.info(option.master)
            role.master = option.master.toLong()
        }
        role.passwordHash = new Sha1Hash("123123").toHex()
        role.classesId = option.classesId.toLong()
        role.hostId = option.hostId.toLong()
        role.schoolId = option.schoolId.toLong()
        role.school = school
        role.schoolName = URLDecoder.decode(option.schoolName, "UTF-8")
        role.name = URLDecoder.decode(option.name, "UTF-8")
        role.username = URLDecoder.decode(option.username, "UTF-8")
        if(role in Student){
            role.addToRoles(Role.findByName(RoleName.AUTHOR))
        }else if(role in Classer){
            role.addToRoles(Role.findByName(RoleName.CLASSER))
            role.classesName = URLDecoder.decode(option.classesName, "UTF-8")
        }else{
            role.addToRoles(Role.findByName(RoleName.TEACHER))
        }
        
        if (role.hasErrors() || !role.save(flush: true)) {
            return ['success':false,'message' : 'BLOG没有保存成功！']
        }else{
            if(role in Student){
                Parent parent = new Parent()
                parent.student = role
                parent.passwordHash = new Sha1Hash("123123").toHex()
                parent.name = role.name+"家长"
                parent.schoolName = role.schoolName
                parent.school = school
                parent.username = role.username+"家长"
                parent.addToRoles(Role.findByName(RoleName.PARENT))
                parent.save(flush: true)
                 if (parent.hasErrors() || !parent.save(flush: true)) {
                      return ['success':false,'message' : '孩子家长没有保存成功！']
                 }
                 role.parent = parent
                 role.save(flush:true)
            }
            saveSite(option,role)
        }
    }

    def saveSite(option,author){
        Site site = new Site(option)
        site.title = URLDecoder.decode(option.title, "UTF-8")
        site.welcomeMessage = URLDecoder.decode(option.welcomeMessage, "UTF-8")
        site.author = author
        if (site.hasErrors() || !site.save(flush: true)) {
            return ['success':false,'message' : 'BLOG没有保存成功！']
        }
        Post post = new Post(
                        title: "${author.name}的第一博！",
                        content: "欢迎来到我的博客！",
                        displayDate: new Date(),
                        author:author,
                        school:author.schoolId,
                        classesid:author.classesId,
                        site:site)
        author.site = site
        author.addToPosts(post)
        author.save()
        if(author in Student){
            def parent = author.parent
            log.info(parent)
            parent.site = site
            parent.save()
        }
    }
}
